package com.backup;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import com.backup.bean.FileObject;
import com.backup.util.FileUtil;

public class CompareFolder {

	public static void main(String[] args)throws IOException{
		long now = new Date().getTime();
		CompareFolder cf = new CompareFolder();
		String srcPath = "E:/SVN/2017_东蓝数码_政务研发中心_云上息烽美丽乡村政务平台/.svn";
		String targetPath = "E:\\_tmp2";
		System.out.println(File.separator);
		FileObject fo = cf.readFileObject(new File(srcPath),  new File(targetPath));
		cf.startCompare(fo);
		System.out.println((new Date().getTime()-now)/1000);
	}
 
	public void startCompare(FileObject fb) throws IOException{
		//先进行文件的拷贝
		for(File file : fb.getChildrenFile()){
			File targetFile = new File( fb.getTargetPathStr() + File.separator + file.getName());
			if(!targetFile.exists() || file.lastModified()!=targetFile.lastModified()){
				//开始拷贝
				FileUtil.copyFile(file, targetFile);
			}
		}
		//再进行文件夹的拷贝
		for(File file : fb.getChildrenFolder()){
			File targetFile = new File( fb.getTargetPathStr() + File.separator + file.getName());
			if(!targetFile.exists()){
				//开始拷贝
				FileUtil.copyDirectiory(file.getPath(), targetFile.getPath());
			}else{
				//对文件夹进行迭代判断
				FileObject childFB = readFileObject(file, targetFile);
				startCompare(childFB);
			}
		}		
	}
	
	//读取文件对象
	public FileObject readFileObject(File srcFile, File targetFile){
		if(!targetFile.exists()){
			targetFile.mkdirs();
		}
		//读取文件
		FileObject fb = new FileObject();
		fb.setFilename(srcFile.getName());
		fb.setSrcPath(srcFile);
		fb.setTargetPath(targetFile);
		for(File file : srcFile.listFiles()){
			if(file.isDirectory()){
				fb.getChildrenFolder().add(file);
			}else{
				fb.getChildrenFile().add(file);
			}
		}
		return fb;
	}
}
